//index.js
import api from '../../../../utils/api.js'
//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    imageurl: api.imageurl,
    imagenum: 0,
    cookId: null
  },
  onLoad: function (option) {
    var that = this
    var cookId = option.cookId
    that.setData({
      cookId: cookId
    })
  },
  // 上传图片
  uploadPhoto: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      success: function (res) {
        console.log('chooseImage', res);
        wx.uploadFile({
          url: api.host + 'index.php?g=Api&m=Cookbook&a=uploadPhoto',
          filePath: res.tempFilePaths[0],
          name: 'myPhoto',
          formData: {
            'app': 'Meal'
          },
          success: function (rs) {
            console.log('uploadFile', rs);
            if (rs.statusCode == 200) {
              var photo = JSON.parse(rs.data);
              var photos = that.data.images;
              photos.push(photo.myPhoto);
              that.setData({
                images: photos,
                imagenum: photos.length
              })
            }
          },
          fail: function (rs) {
            console.log('fail', rs)
          }
        })
      }
    })
  },
  // 显示图片
  showImage: function (e) {
    console.log('showImage', e)
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [url],
    })
  },
  // 长按图片,删除图片
  longTapImage: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除图片吗？',
      success: function (res) {
        if (res.confirm) {
          var savename = e.currentTarget.dataset.savename;
          var photos = that.data.images;
          for (var i in photos) {
            var photo = photos[i];
            if (photo.savename == savename) {
              photos.splice(i, 1);
            }
          }
          that.setData({
            images: photos
          })
        }
      }
    })
  },
  contentChange: function (e) {
    var content = e.detail.value
    console.log('content', content)
    this.setData({
      content: content
    })
  },
  formsubmit: function () {
    var that = this
    var images = that.data.images;
    var content = that.data.content;
    // 内容为空判断
    if (content == null || content == "") {
      wx.showModal({
        content: "评论内容不能为空",
        confirmText: "确定",
        showCancel: false,
      })
      return;
    }
    var photos = "[";
    for (var i = 0; i < images.length; i++) {
      photos += "{'savename':'" + images[i].savename + "','savepath':'" + images[i].savepath + "'},";
    }
    photos = photos.substr(0, photos.length - 1);
    photos += "]";
    console.log("photos", photos)
    api.addReview({
      data: {
        photos: photos,
        content: content,
        cookId: that.data.cookId,
        ouId: app.globalData.userid
      },
      success: function (res) {
        console.log('反馈', res)
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index',
    }
  },
})